package com.cy.ppj.service.statistics.impl;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cy.ppj.comm.utils.CommonUtils;
import com.cy.ppj.dao.mapper.account.CommissionRecordMapper;
import com.cy.ppj.dao.mapper.order.OrderMapper;
import com.cy.ppj.dao.mapper.order.WithholdRecordMapper;
import com.cy.ppj.dao.mapper.statistics.StatisticsLoanAmountMapper;
import com.cy.ppj.dao.mapper.statistics.StatisticsUserMapper;
import com.cy.ppj.dao.mapper.user.UserMapper;
import com.cy.ppj.model.dto.statistics.OrderLoanRateStatisticsDTO;
import com.cy.ppj.model.query.statistics.ChannelUserGroupByCityStatisticsQuery;
import com.cy.ppj.model.query.statistics.OrderGroupByCityStatisticsQuery;
import com.cy.ppj.model.vo.admin.order.OrderCommissionAdminStatisticsVO;
import com.cy.ppj.model.vo.admin.statistics.ChannelUserGroupByCityStatisticsVO;
import com.cy.ppj.model.vo.admin.statistics.CommissionAndWithholdStatisticsVO;
import com.cy.ppj.model.vo.admin.statistics.OrderGroupByCityStatisticsVO;
import com.cy.ppj.model.vo.admin.statistics.OrderLoanRateStatisticsVO;
import com.cy.ppj.model.vo.admin.statistics.StatisticsOrderLoanCountVO;
import com.cy.ppj.model.vo.admin.statistics.StatisticsOrderLoanVO;
import com.cy.ppj.model.vo.admin.statistics.StatisticsUserCountVO;
import com.cy.ppj.model.vo.admin.statistics.StatisticsUserVO;
import com.cy.ppj.model.vo.admin.statistics.UserStructureStatisticsVO;
import com.cy.ppj.service.BaseService;
import com.cy.ppj.service.statistics.DataStatisticsService;

/**
 * 后台-数据统计
 *
 * @author: tanyilin
 * @create: 2018年10月17日 上午11:57:45
 */
@Service
public class DataStatisticsServiceImpl extends BaseService implements DataStatisticsService{

	@Autowired
	private StatisticsUserMapper statisticsUserMapper;
	@Autowired
	private StatisticsLoanAmountMapper statisticsLoanAmountMapper;
	@Autowired
	private WithholdRecordMapper withholdRecordMapper;
	@Autowired
	private CommissionRecordMapper commissionRecordMapper;
	@Autowired
	private UserMapper userMapper;
	@Autowired
	private OrderMapper orderMapper;
	
	@Override
	public List<StatisticsUserVO> selectStatisticsUserVOs() {
		SimpleDateFormat sdf = new SimpleDateFormat("MM-dd");
		List<StatisticsUserVO> newList = new ArrayList<>();
		Date endDate = new Date();
		Date startDate = CommonUtils.addTime(endDate, -30, Calendar.DAY_OF_YEAR);
		startDate = CommonUtils.getDayStartTime(startDate);
		List<StatisticsUserVO> oldList = statisticsUserMapper.selectStatisticsUserVO(startDate, endDate);
		int n = CommonUtils.diffDays(startDate, endDate);
		int num = CommonUtils.isEmpty(oldList) ? 0 : oldList.size();
		int temp = 0;
		for(int i = 0; i < n; i++) {
			if(temp < num) {
				if(startDate.compareTo(oldList.get(temp).getDate()) < 0) {
					StatisticsUserVO vo = new StatisticsUserVO();
					vo.setCount(0);
					vo.setDateFormatStr(sdf.format(startDate));
					newList.add(vo);
				}else if(startDate.compareTo(oldList.get(temp).getDate()) == 0) {
					StatisticsUserVO vo = oldList.get(temp);
					vo.setDateFormatStr(sdf.format(vo.getDate()));
					vo.setDate(null);
					newList.add(vo);
					temp++;
				}
			}else if(temp >= num && startDate.compareTo(endDate) <= 0) {
				StatisticsUserVO vo = new StatisticsUserVO();
				vo.setCount(0);
				vo.setDateFormatStr(sdf.format(startDate));
				newList.add(vo);
			}
			startDate = CommonUtils.addTime(startDate, 1, Calendar.DAY_OF_YEAR);
		}
		
		return newList;
	}

	@Override
	public StatisticsUserCountVO selectStatisticsUserCountVO() {
		
		Date endDate = new Date();
		Date startDate = CommonUtils.getDayStartTime(CommonUtils.addTime(endDate, -7, Calendar.DAY_OF_YEAR));
		int sevenDayCount = statisticsUserMapper.selectSumUserCountByTime(startDate, endDate);
		startDate = CommonUtils.getDayStartTime(CommonUtils.addTime(endDate, -30, Calendar.DAY_OF_YEAR));
		int thirtyDayCount = statisticsUserMapper.selectSumUserCountByTime(startDate, endDate);
		int allCount = statisticsUserMapper.selectAllSumUserCount();
		
		StatisticsUserCountVO statisticsUserCountVO = new StatisticsUserCountVO();
		statisticsUserCountVO.setAllCount(allCount);
		statisticsUserCountVO.setSevenDayCount(sevenDayCount);
		statisticsUserCountVO.setThirtyDayCount(thirtyDayCount);
		
		return statisticsUserCountVO;
	}

	@Override
	public List<StatisticsOrderLoanVO> selectStatisticsOrderLoanVOs() {
		SimpleDateFormat sdf = new SimpleDateFormat("MM-dd");
		List<StatisticsOrderLoanVO> newList = new ArrayList<>();
		Date endDate = new Date();
		Date startDate = CommonUtils.addTime(endDate, -15, Calendar.DAY_OF_YEAR);
		startDate = CommonUtils.getDayStartTime(startDate);
		List<StatisticsOrderLoanVO> oldList = statisticsLoanAmountMapper.selectStatisticsOrderLoanVOs(startDate, endDate);
		int n = CommonUtils.diffDays(startDate, endDate);
		int num = CommonUtils.isEmpty(oldList) ? 0 : oldList.size();
		int temp = 0;
 		
		for(int i = 0; i < n; i++) {
			if(temp < num) {
				if(startDate.compareTo(oldList.get(temp).getDate()) < 0) {
					StatisticsOrderLoanVO vo = new StatisticsOrderLoanVO();
					vo.setLoanCount(0);
					vo.setLoanAmount(BigDecimal.ZERO);
					vo.setDateFormatStr(sdf.format(startDate));
					newList.add(vo);
				}else if(startDate.compareTo(oldList.get(temp).getDate()) == 0) {
					StatisticsOrderLoanVO vo = oldList.get(temp);
					vo.setDateFormatStr(sdf.format(vo.getDate()));
					vo.setDate(null);
					vo.setOrderCount(null);
					newList.add(vo);
					temp++;
				}
			}else if(temp >= num && startDate.compareTo(endDate) <= 0) {
				StatisticsOrderLoanVO vo = new StatisticsOrderLoanVO();
				vo.setLoanCount(0);
				vo.setLoanAmount(BigDecimal.ZERO);
				vo.setDateFormatStr(sdf.format(startDate));
				newList.add(vo);
			}
			startDate = CommonUtils.addTime(startDate, 1, Calendar.DAY_OF_YEAR);
		}
		
		return newList;
	}

	@Override
	public StatisticsOrderLoanCountVO selectStatisticsOrderLoanCountVO() {
		Date endTime = new Date();
		Date sevenDayStartTime = CommonUtils.getDayStartTime(CommonUtils.addTime(endTime, -7, Calendar.DAY_OF_YEAR));
		Date thirtyDayStartTime = CommonUtils.getDayStartTime(CommonUtils.addTime(endTime, -30, Calendar.DAY_OF_YEAR));
		StatisticsOrderLoanCountVO statisticsOrderLoanCountVO = statisticsLoanAmountMapper.selectStatisticsOrderLoanCountVO(sevenDayStartTime, thirtyDayStartTime, endTime);
		statisticsOrderLoanCountVO.setAllOrderCount(null);
		statisticsOrderLoanCountVO.setSevenDayOrderCount(null);
		statisticsOrderLoanCountVO.setThirtyDayOrderCount(null);
		return statisticsOrderLoanCountVO;
	}

	@Override
	public List<StatisticsOrderLoanVO> selectStatisticsOrderVOs() {
		SimpleDateFormat sdf = new SimpleDateFormat("MM-dd");
		List<StatisticsOrderLoanVO> newList = new ArrayList<>();
		Date endDate = new Date();
		Date startDate = CommonUtils.addTime(endDate, -30, Calendar.DAY_OF_YEAR);
		startDate = CommonUtils.getDayStartTime(startDate);
		List<StatisticsOrderLoanVO> oldList = statisticsLoanAmountMapper.selectStatisticsOrderLoanVOs(startDate, endDate);
		int n = CommonUtils.diffDays(startDate, endDate);
		int num = CommonUtils.isEmpty(oldList) ? 0 : oldList.size();
		int temp = 0;
		
		for(int i = 0; i < n; i++) {
			if(temp < num) {
				if(startDate.compareTo(oldList.get(temp).getDate()) < 0) {
					StatisticsOrderLoanVO vo = new StatisticsOrderLoanVO();
					vo.setOrderCount(0);
					vo.setDateFormatStr(sdf.format(startDate));
					newList.add(vo);
				}else if(startDate.compareTo(oldList.get(temp).getDate()) == 0) {
					StatisticsOrderLoanVO vo = oldList.get(temp);
					vo.setDateFormatStr(sdf.format(vo.getDate()));
					vo.setDate(null);
					vo.setLoanAmount(null);
					vo.setLoanCount(null);
					newList.add(vo);
					temp++;
				}
			}else if(temp >= num && startDate.compareTo(endDate) <= 0) {
				StatisticsOrderLoanVO vo = new StatisticsOrderLoanVO();
				vo.setOrderCount(0);
				vo.setDateFormatStr(sdf.format(startDate));
				newList.add(vo);
			}
			startDate = CommonUtils.addTime(startDate, 1, Calendar.DAY_OF_YEAR);
		}
		
		return newList;
	}

	@Override
	public StatisticsOrderLoanCountVO selectStatisticsOrderCountVO() {
		Date endTime = new Date();
		Date sevenDayStartTime = CommonUtils.getDayStartTime(CommonUtils.addTime(endTime, -7, Calendar.DAY_OF_YEAR));
		Date thirtyDayStartTime = CommonUtils.getDayStartTime(CommonUtils.addTime(endTime, -30, Calendar.DAY_OF_YEAR));
		StatisticsOrderLoanCountVO statisticsOrderLoanCountVO = statisticsLoanAmountMapper.selectStatisticsOrderLoanCountVO(sevenDayStartTime, thirtyDayStartTime, endTime);
		statisticsOrderLoanCountVO.setAllLoanAmount(null);
		statisticsOrderLoanCountVO.setAllLoanCount(null);
		statisticsOrderLoanCountVO.setSevenDayLoanAmount(null);
		statisticsOrderLoanCountVO.setSevenDayLoanCount(null);
		statisticsOrderLoanCountVO.setThirtyDayLoanAmount(null);
		statisticsOrderLoanCountVO.setThirtyDayLoanCount(null);
		return statisticsOrderLoanCountVO;
	}

	@Override
	public CommissionAndWithholdStatisticsVO selectCommissionAndWithholdStatisticsVO() {
		CommissionAndWithholdStatisticsVO vo = withholdRecordMapper.selectWithholdStatisticsVO();
		OrderCommissionAdminStatisticsVO commission = commissionRecordMapper.selectOrderCommissionAdminStatistics();
		vo.setCommissionCount(commission.getCount());
		vo.setCommissionAmount(commission.getAmount());
		return vo;
	}

	@Override
	public List<UserStructureStatisticsVO> selectUserStructureStatisticsVOs() {
		List<UserStructureStatisticsVO> list = userMapper.selectUserStructureStatisticsVOs();
		list.stream().forEach(n -> {
			n.setItem(n.getType().getText());
			n.setType(null);
		});
		return list;
	}

	@Override
	public List<OrderLoanRateStatisticsVO> selectOrderLoanRateStatisticsVOs() {
		OrderLoanRateStatisticsDTO dto = orderMapper.selectOrderLoanRateStatisticsDTO();
		// 合计提单
		OrderLoanRateStatisticsVO orderCount = new OrderLoanRateStatisticsVO();
		orderCount.setItem("合计提单");
		orderCount.setCount(dto.getOrderCount());
		// 合计放款
		OrderLoanRateStatisticsVO loanCount = new OrderLoanRateStatisticsVO();
		loanCount.setItem("合计放款");
		loanCount.setCount(dto.getLoanCount());
		
		List<OrderLoanRateStatisticsVO> list = new ArrayList<>();
		list.add(orderCount);
		list.add(loanCount);
		
		return list;
	}

	@Override
	public List<OrderGroupByCityStatisticsVO> selectOrderLoanAmountGroupByCityStatisticsVOs(
			OrderGroupByCityStatisticsQuery query) {
		List<OrderGroupByCityStatisticsVO> list = orderMapper.selectOrderLoanAmountGroupByCityStatisticsVOs(query);
		return list;
	}

	@Override
	public List<OrderGroupByCityStatisticsVO> selectOrderCountGroupByCityStatisticsVOs(
			OrderGroupByCityStatisticsQuery query) {
		List<OrderGroupByCityStatisticsVO> list = orderMapper.selectOrderCountGroupByCityStatisticsVOs(query);
		return list;
	}

	@Override
	public List<ChannelUserGroupByCityStatisticsVO> selectchannelUserGroupByCityStatisticsVOs(
			ChannelUserGroupByCityStatisticsQuery query) {
		List<ChannelUserGroupByCityStatisticsVO> list = userMapper.selectchannelUserGroupByCityStatisticsVOs(query);
		return list;
	}

}
